/*
题目链接:https://leetcode.cn/problems/count-the-number-of-good-subarrays/submissions/622786763/?envType=daily-question&envId=2025-04-16
*/

//题解代码:
class Solution {
public:
    #define ll long long
    long long countGood(vector<int>& nums, int k) {
        int n = nums.size();
        unordered_map<int,int> ump;
        ll ans = 0, x = 0;
        for(int l=0,r=0;r<n;++r){
            x += ump[nums[r]];
            ump[nums[r]]++;
            while(x>=k){
                if(x-ump[nums[l]]+1 < k) break;
                x -= (ump[nums[l]]-1);
                ump[nums[l++]]--;
            }
            if(x>=k) ans += l+1;
        }
        return ans;
    }
};
